package com.sfj.system.controller;

import com.kaka.starter.control.dto.ResultBean;
import com.sfj.system.dto.userinfo.UserInfoAddDTO;
import com.sfj.system.dto.userinfo.UserInfoQueryDTO;
import com.sfj.system.dto.userinfo.UserInfoUpdDTO;
import com.sfj.system.entity.UserInfoEntity;
import com.sfj.system.service.userinfo.UserInfoQueryService;
import com.sfj.system.service.userinfo.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 操作员信息Controller
 *
 * @author kaka
 * @date 2020-09-24
 */
@Api(tags = "操作员信息")
@RestController
@RequestMapping("user")
public class UserInfoController {

    @Autowired
    private UserInfoService userinfoService;

    @Autowired
    private UserInfoQueryService userinfoQueryService;

    @PostMapping("/getUserNameByIds")
    @ApiOperation(value = "根据ids获取用户名", notes = "根据ids获取用户名")
    public ResultBean getUserNameByIds(@RequestBody List<Integer> userIds) {
        return ResultBean.succeed(userinfoQueryService.getUserNameByIds(userIds));
    }

    @PostMapping("/query")
    @ApiOperation(value = "操作员信息查询", notes = "操作员信息查询")
    public ResultBean query(@RequestBody UserInfoQueryDTO queryReq) {
        return ResultBean.succeed(userinfoQueryService.queryUserInfoList(queryReq));
    }

    @PostMapping("/save")
    @ApiOperation(value = "操作员信息新增", notes = "操作员信息新增")
    public ResultBean save(@Valid @RequestBody UserInfoAddDTO record) {
        userinfoService.save(record);
        return ResultBean.succeed();
    }

    @PostMapping("/upd")
    @ApiOperation(value = "操作员信息修改", notes = "操作员信息修改")
    public ResultBean upd(@Valid @RequestBody UserInfoUpdDTO record) {
        userinfoService.upd(record);
        return ResultBean.succeed();
    }

    @PostMapping("/del")
    @ApiOperation(value = "操作员信息删除", notes = "操作员信息删除")
    public ResultBean del(@RequestBody Integer id) {
        userinfoService.del(id);
        return ResultBean.succeed();
    }

    @PostMapping("/queryUserById")
    @ApiOperation(value = "根据ID查询用户信息", notes = "根据ID查询用户信息")
    public ResultBean getByUserId(@RequestParam Integer userId) {
        UserInfoEntity entity = userinfoQueryService.getById(userId);
        if(entity==null){
            return ResultBean.fail("用户不存在");
        }else {
            return ResultBean.succeed(entity);
        }

    }

    @GetMapping("/validUserByPoliceNum")
    @ApiOperation(value = "判断警号是否重复", notes = "判断警号是否重复")
    public ResultBean validUserByPoliceNum(@RequestParam String policeNum, @RequestParam Integer userId) {
        userinfoService.validUserByPoliceNum(policeNum,userId);
        return ResultBean.succeed();
    }

    @PostMapping("/import")
    @ApiOperation(value = "用户管理导入xls信息", notes = "用户管理导入xls信息")
    public ResultBean importUserXls(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return ResultBean.fail("上传文件为空");
        }

        String message = userinfoService.importFromExcel(file);
        if("失败".equals(message)){
            return ResultBean.fail("导入文件失败");
        }else {
            return ResultBean.succeed(message);

        }
    }

    @GetMapping("/queryByTask")
    @ApiOperation(value = "可分配任务的人员查询", notes = "可分配任务的人员查询")
    public ResultBean queryByTask() {
        return ResultBean.succeed(userinfoQueryService.queryUserByTask(Arrays.asList(5,6,7)));
    }

    @GetMapping("/queryByReview")
    @ApiOperation(value = "可审核任务的人员查询", notes = "可审核任务的人员查询")
    public ResultBean queryUserByReview() {
        return ResultBean.succeed(userinfoQueryService.queryUserByTask(Arrays.asList(2)));
    }


}